package com.wanchuang.qhbb.adapter;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.wanchuang.qhbb.R;
import com.ruffian.library.widget.RRadioButton;
import com.wanchuang.qhbb.proto.AgentPolicy;
import com.wanchuang.qhbb.utils.ValidateUtils;

import java.util.List;

public class PolicyItemAdapter extends BaseMultiItemQuickAdapter<PolicyItemBean, BaseViewHolder> {

    public PolicyItemAdapter(List<PolicyItemBean> data) {
        super(data);
        // 绑定 layout 对应的 type
        addItemType(PolicyItemBean.TITLE, R.layout.item_policy_item_title);
        addItemType(PolicyItemBean.CONTENT_ONE, R.layout.item_policy_item_number);
        addItemType(PolicyItemBean.CONTENT_TWO, R.layout.item_policy_item_check);
        addItemType(PolicyItemBean.CONTENT_THREE, R.layout.item_policy_item_select);
        addItemType(PolicyItemBean.CONTENT_FOUR, R.layout.item_policy_item_null);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, PolicyItemBean item) {
        // 根据返回的 type 分别设置数据
        switch (helper.getItemViewType()) {
            case 0:
                helper.setText(R.id.tv_goods_name, item.getPolicy_name());
                break;
            case 1:
                helper.setText(R.id.tv_goods_name, item.getPolicy_name());
                if (item.getType().equals("1")) {
                    helper.setText(R.id.tv_number, item.getPolicy_value() + "元");
                } else {
                    helper.setText(R.id.tv_number, item.getPolicy_value() + "%");
                }
                break;
            case 2:
                helper.setText(R.id.tv_goods_name, item.getPolicy_name());
                if (item.getPolicy_value().equals("0")) {
                    helper.<RRadioButton>getView(R.id.rb_one).setChecked(true);
                } else {
                    helper.<RRadioButton>getView(R.id.rb_two).setChecked(true);
                }
                helper.<RRadioButton>getView(R.id.rb_one).setOnClickListener(v -> {
                    item.setPolicy_value("0");
                });
                helper.<RRadioButton>getView(R.id.rb_two).setOnClickListener(v -> {
                    item.setPolicy_value("1");
                });
                break;
            case 3:
                if (ValidateUtils.isValidate(item.getPolicy_value())) {
                    String value = item.getPolicy_value();
                    for (AgentPolicy.value_enum value_enum : item.getValueEnum()) {
                        if (value_enum.getValue().equals(value)) {
                            helper.setText(R.id.tv_value, value_enum.getName());
                            break;
                        } else {
                            helper.setText(R.id.tv_value, "请选择");
                        }
                    }

                } else {
                    helper.setText(R.id.tv_value, "请选择");
                }
                helper.setText(R.id.tv_goods_name, item.getPolicy_name());

                break;
            default:
                break;
        }
    }
}